package logic;
import java.io.Serializable;
import java.util.Vector;


public class Chunk implements Serializable, Comparable<Chunk>{
	/**
	 *  
	 */
	private static final long serialVersionUID = 1L;
	private String fileID;
	private int chunckNo;
	private int replicationDeg;
	private Vector<String> ips; 
	public Chunk(String _fileID, int _chunkNo, int _replicationDeg)
	{
		this.fileID=_fileID;
		this.chunckNo=_chunkNo;
		this.replicationDeg=_replicationDeg;
		this.setIps(new Vector<String>());

	}
	public void addIP(String str)
	{
		if(!ips.contains(str))
		{
			ips.add(str);
		}
	}
	public Chunk(String _fileID, int _chunkNo)
	{
		this.fileID=_fileID;
		this.chunckNo=_chunkNo;
		this.replicationDeg=0;
	}
	public String getFileID() {
		return fileID;
	}
	public void setFileID(String fileID) {
		this.fileID = fileID;
	}
	public int getChunkNo() {
		return chunckNo;
	}
	public void setChunkNo(int chunckNo) {
		this.chunckNo = chunckNo;
	}
	public int getReplicationDeg() {
		return replicationDeg;
	}
	public void setReplicationDeg(int replicationDeg) {
		this.replicationDeg = replicationDeg;
	}
	@Override 
	public boolean equals(Object o) {
		if(this.getFileID().equals(((Chunk)o).getFileID()) 
				&& this.getChunkNo()==((Chunk)o).getChunkNo())
			return true;
		else return false;
	}
	public Vector<String> getIps() {
		return ips;
	}
	public void setIps(Vector<String> ips) {
		this.ips = ips;
	}
	public boolean removeIP(String ip)
	{
		return ips.remove(ip);
	}
	@Override
	public int compareTo(Chunk o) {// lista de chunks por ordem de prioridade para remover
		int size = this.getIps().size();
		int o_size = o.getIps().size();
		if((size > this.getReplicationDeg() && o_size > o.getReplicationDeg())
				||(size == this.getReplicationDeg() && o_size == o.getReplicationDeg())
				||(size < this.getReplicationDeg() && o_size < o.getReplicationDeg())){
			
			if(this.ips.size()>o.getIps().size())
				return -1;
			else if(this.ips.size()==o.getIps().size())
				return 0;
			else return 1;
		}
		else if(size > this.getReplicationDeg() && o_size <= o.getReplicationDeg())
			return -1;
		else if(size <= this.getReplicationDeg() && o_size > o.getReplicationDeg())
			return 1;
		
		return 0;
	}
}
